package cn.iocoder.yudao.module.ent.controller.admin.followrecord;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.ent.controller.admin.followrecord.vo.FollowRecordPageReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.followrecord.vo.FollowRecordRespVO;
import cn.iocoder.yudao.module.ent.controller.admin.followrecord.vo.FollowRecordSaveReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.receivenumber.vo.ReceiveNumberRespVO;
import cn.iocoder.yudao.module.ent.convert.followrecord.FollowRecordConverter;
import cn.iocoder.yudao.module.ent.convert.receivenumber.ReceiveNumberConverter;
import cn.iocoder.yudao.module.ent.dal.dataobject.followrecord.FollowRecordDO;
import cn.iocoder.yudao.module.ent.dal.dataobject.tag.TagDO;
import cn.iocoder.yudao.module.ent.service.followrecord.FollowRecordService;
import cn.iocoder.yudao.module.ent.service.search.SearchService;
import cn.iocoder.yudao.module.ent.service.tag.TagService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "企业大数据 - 跟进记录")
@RestController
@RequestMapping("/ent/follow-record")
@Validated
public class FollowRecordController {

    @Resource
    private FollowRecordService followRecordService;

    @Resource
    private TagService tagService;

    @Resource
    private SearchService searchService;

    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建跟进记录")
    @PreAuthorize("@ss.hasPermission('ent:follow-record:create')")
    public CommonResult<Long> createFollowRecord(@Valid @RequestBody FollowRecordSaveReqVO createReqVO) {
        return success(followRecordService.createFollowRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跟进记录")
    @PreAuthorize("@ss.hasPermission('ent:follow-record:update')")
    public CommonResult<Boolean> updateFollowRecord(@Valid @RequestBody FollowRecordSaveReqVO updateReqVO) {
        followRecordService.updateFollowRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跟进记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ent:follow-record:delete')")
    public CommonResult<Boolean> deleteFollowRecord(@RequestParam("id") Long id) {
        followRecordService.deleteFollowRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跟进记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ent:follow-record:query')")
    public CommonResult<FollowRecordRespVO> getFollowRecord(@RequestParam("id") Long id) {
        FollowRecordDO followRecord = followRecordService.getFollowRecord(id);
        return success(BeanUtils.toBean(followRecord, FollowRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跟进记录分页")
    @PreAuthorize("@ss.hasPermission('ent:follow-record:query')")
    public CommonResult<PageResult<FollowRecordRespVO>> getFollowRecordPage(@Valid FollowRecordPageReqVO pageReqVO) {
        PageResult<FollowRecordDO> pageResult = followRecordService.getFollowRecordPage(pageReqVO);
        List<TagDO> tagList = tagService.getTagList();
        Map<String, String> nameMap = searchService.getEntCompanyNameMap(CollectionUtils.convertList(pageResult.getList(), FollowRecordDO::getEntId));
//        Map<Long, AdminUserRespDTO> userMap = adminUserApi.get
        return success(FollowRecordConverter.INSTANCE.convert0(BeanUtils.toBean(pageResult, FollowRecordRespVO.class), tagList, nameMap));
//        return success(BeanUtils.toBean(pageResult, FollowRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跟进记录 Excel")
    @PreAuthorize("@ss.hasPermission('ent:follow-record:export')")
    @OperateLog(type = EXPORT)
    public void exportFollowRecordExcel(@Valid FollowRecordPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FollowRecordDO> list = followRecordService.getFollowRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "跟进记录.xls", "数据", FollowRecordRespVO.class,
                BeanUtils.toBean(list, FollowRecordRespVO.class));
    }

}